<?php


namespace app\api\model\User;


use think\facade\Db;
use think\Model;

class UserCouponModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "user_coupon";

    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getUserCouponWhere($where,$field="*")
    {
        $coupon = Db::name("user_coupon")->field($field)->where($where)->find();
        return $coupon;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @param $field
     * @return mixed
     * @throws
     */
    public static function getUserCouponWhereMore($where,$field="*")
    {
        $list = Db::name("user_coupon")->field($field)->where($where)->select()->toArray();
        return $list;
    }
    /**
     * 根据条件查询多条数据
     * @param $where
     * @param $whereOr
     * @param $field
     * @return mixed
     * @throws
     */
    public static function getUserCouponWhereOrMore($where,$field="*")
    {
        $list = Db::name("user_coupon")->field($field)->whereOr($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getUserCouponCountWhere($where)
    {
        $count = Db::name("user_coupon")->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @param $field
     * @return mixed
     * @throws
     */
    public static function getUserCouponListWhere($page,$limit,$where = [],$field="*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('user_coupon')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->order('add_time desc')
            ->select()->toArray();
        return $list;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateUserCouponWhere($where,$save)
    {
        $flag = Db::name('user_coupon')->where($where)->update($save);
        return $flag;
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public static function addUserCoupon($data)
    {
        $id = Db::name('user_coupon')->insertGetId($data);
        return $id;
    }


}